package ev.data;//need to add this line

import java.io.*;
import java.util.*;

public class MyData {
	private StreamTokenizer st;

	public static ArrayList<City> cities = new ArrayList<City>();

	public MyData(String filename) {
		readAndParseFile(filename);
	}

	public static ArrayList<City> getCityData() {
		return cities;
	}


	// Method readAndParseFile, reads the text file, which contains data
	private void readAndParseFile(String filename) {
		if(identifyFile(filename)){
			System.out.println("TRUE");
			specialFile(filename);
		}else{
			openFile(filename);
			String startMark="";
			while (st.ttype != StreamTokenizer.TT_EOF) {
	
				if (st.ttype == StreamTokenizer.TT_WORD) {
					startMark=st.sval;
					next();
				}
				//System.out.println(startMark);
				if(!startMark.equals("SECTION")){
					next();continue;
				}
	
				if (st.ttype == StreamTokenizer.TT_NUMBER) {
	
					int cityId = (int)st.nval;
					//System.out.println("id="+cityId);
					next();
	
					double x_coordinator = st.nval;
					//System.out.println("x="+x_coordinator);
					next();
	
					double y_coordinator = st.nval;
					//System.out.println("y="+y_coordinator);
					next();
	
					City c = new City(x_coordinator, y_coordinator, cityId);
					cities.add(c);
				} else {
					next();
				}
			}
		}
	}
	
	private boolean identifyFile(String filename) {
		boolean scientificNotation=false;
		openFile(filename);
		String startMark="";
		while (st.ttype != StreamTokenizer.TT_EOF) {

			if (st.ttype == StreamTokenizer.TT_WORD) {
				startMark=st.sval;
				if(startMark.equals("e")){
					scientificNotation=true;
					break;
				}
				next();
			} else {
				next();
			}
		}
		return scientificNotation;
	}
	
	private void specialFile(String filename) {
		String s;
		try {
			FileReader f=new FileReader(filename);
			BufferedReader br=new BufferedReader(f);
			try {
				br.readLine();
				br.readLine();
				br.readLine();
				br.readLine();
				br.readLine();
				br.readLine();
				s=br.readLine();
				//System.out.println("s="+s);
				while(!s.equals("EOF")){					
					//System.out.println("s="+s);
					String[] sa=trim(s);
					//for(int i=0;i<sa.length;i++){
						//System.out.print(sa[i]+"     ");
					//}
					//System.out.println("saSize="+sa.length);
					int cityId=Integer.parseInt(sa[0]);
					
					String[] sb=sa[1].split("e");
					//for(int i=0;i<sb.length;i++){
						//System.out.print(sb[i]+"     ");
					//}
					double x=Double.parseDouble(sb[0]);
					//System.out.println("x="+x);
					double expX=Double.parseDouble(sb[1]);
					//System.out.println("expX="+expX);
					double x_coordinator=x*(Math.pow(10,expX));
					//System.out.println("x_coordinator="+x_coordinator);
					
					String[] sc=sa[2].split("e");
					//for(int i=0;i<sc.length;i++){
						//System.out.print(sc[i]+"     ");
					//}
					double y=Double.parseDouble(sc[0]);
					//System.out.println("y="+y);
					double expY=Double.parseDouble(sc[1]);
					//System.out.println("expY="+expY);
					double y_coordinator=y*(Math.pow(10,expY));
					//System.out.println("y_coordinator="+y_coordinator);
					
					City c = new City(x_coordinator, y_coordinator, cityId);
					cities.add(c);
					//System.out.println("size="+cities.size());
					
					s=br.readLine();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private String[] trim(String s){
		s=s.trim();
		String[] sa=s.split(" ");
		return sa;
	}
	/*
	 * Get the next number from the open file
	 */
	private boolean next() {
		boolean flag = true;
		try {
			st.nextToken();
		} catch (IOException e) {
			System.err
					.println("Error: Failed to get next number.Is the file open?");
			System.exit(-1);
			flag = false;
		}
		return flag;
	}

	private void openFile(String filename) {
		try {
			st = new StreamTokenizer(new FileReader(filename));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.err.println("File \"" + filename + "\" not found");
			System.exit(0);
		}
	}

	public String getDataSource() {
		return null;
	}
}